<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class toko extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library("phpexcel");
        $this->load->library("PHPExcel/iofactory");
    }

    function hutang()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Hutang Toko');
        $this->load->view('pengurus/br-08/toko/hutang');
    }

    function rugi_laba()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Rugi Laba Toko');
        $this->load->view('pengurus/br-08/toko/rugi_laba');
    }

    function rugi_laba_date()
    {
        $date = $this->input->post("TGL");

        $this->load->model('crud/m_transaksi_kasir');
        $penjualan = $this->m_transaksi_kasir->get_total_penjualan("`TGL_TRANSAKSI` like '$date%'")->first_row()->total;

        $this->load->model('crud/m_order_barang');
        $pembelian = $this->m_order_barang->get_total_pembelian("ID_FAKTUR != 0 AND TGL_ORDER like '$date%'")->first_row()->total;

        $this->load->model('report/m_barang');
        $stok_awal = $this->m_barang->jumlah_stok_awal()->first_row()->total;
        $stok_akhir = $this->m_barang->jumlah_stok_akhir()->first_row()->total;

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Laba Rugi Bulanan Toko Bulan $date")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A4'), 'B4:E4' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");

        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:E1');
        $sheet->mergeCells('A2:E2');
        $sheet->mergeCells('A3:E3');
        $sheet->mergeCells('A4:E4');
        
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('B6', "LAPORAN RUGI LABA : $date");
        $sheet->mergeCells('B6:C6');

        $objPHPExcel->getActiveSheet()->getStyle('C8')->getNumberFormat()->setFormatCode('Rp#,##');
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('C8'), 'C9:C16' );

        $objPHPExcel->getActiveSheet()->setCellValue('B8', "Stok Awal :");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', $stok_awal);
        
        $objPHPExcel->getActiveSheet()->setCellValue('B9', "Pembelian :");
        $objPHPExcel->getActiveSheet()->setCellValue('C9', $pembelian);

        $objPHPExcel->getActiveSheet()->getStyle('C9')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D9', "+");

        $objPHPExcel->getActiveSheet()->setCellValue('B10', "Tersedia dijual :");
        $objPHPExcel->getActiveSheet()->setCellValue('C10', "=C8+C9");

        $objPHPExcel->getActiveSheet()->setCellValue('B11', "Stok Akhir :");
        $objPHPExcel->getActiveSheet()->setCellValue('C11', $stok_akhir);

        $objPHPExcel->getActiveSheet()->getStyle('C11')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D11', "-");

        $objPHPExcel->getActiveSheet()->setCellValue('B12', "HPP :");
        $objPHPExcel->getActiveSheet()->setCellValue('C12', "=C10-C11");

        $objPHPExcel->getActiveSheet()->setCellValue('B14', "Penjualan :");
        $objPHPExcel->getActiveSheet()->setCellValue('C14', "$penjualan");

        $objPHPExcel->getActiveSheet()->setCellValue('B15', "HPP :");
        $objPHPExcel->getActiveSheet()->setCellValue('C15', "=C12");

        $objPHPExcel->getActiveSheet()->getStyle('C15')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D15', "-");

        $objPHPExcel->getActiveSheet()->setCellValue('B16', "Rugi/Laba :");
        $objPHPExcel->getActiveSheet()->setCellValue('C16', "=C14-C15");

        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader("&L&G&C&HLaporan Rugi Laba");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=Laporan_Rugi_Laba_Toko_[$date]");
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

    function hutang_date()
    {
        $date = $this->input->post("TGL");

        $this->load->model('crud/m_transaksi_kasir');
        $penjualan = $this->m_transaksi_kasir->get_total_penjualan("`TGL_TRANSAKSI` like '$date%'")->first_row()->total;

        $this->load->model('crud/m_order_barang');
        $pembelian = $this->m_order_barang->get_total_pembelian("ID_FAKTUR != 0 AND TGL_ORDER like '$date%'")->first_row()->total;

        $this->load->model('report/m_barang');
        $stok_awal = $this->m_barang->jumlah_stok_awal()->first_row()->total;
        $stok_akhir = $this->m_barang->jumlah_stok_akhir()->first_row()->total;

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Laba Rugi Bulanan Toko Bulan $date")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A4'), 'B4:E4' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");

        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:E1');
        $sheet->mergeCells('A2:E2');
        $sheet->mergeCells('A3:E3');
        $sheet->mergeCells('A4:E4');

        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('B6', "LAPORAN RUGI LABA : $date");
        $sheet->mergeCells('B6:C6');

        $objPHPExcel->getActiveSheet()->getStyle('C8')->getNumberFormat()->setFormatCode('Rp#,##');
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('C8'), 'C9:C16' );

        $objPHPExcel->getActiveSheet()->setCellValue('B8', "Stok Awal :");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', $stok_awal);

        $objPHPExcel->getActiveSheet()->setCellValue('B9', "Pembelian :");
        $objPHPExcel->getActiveSheet()->setCellValue('C9', $pembelian);

        $objPHPExcel->getActiveSheet()->getStyle('C9')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D9', "+");

        $objPHPExcel->getActiveSheet()->setCellValue('B10', "Tersedia dijual :");
        $objPHPExcel->getActiveSheet()->setCellValue('C10', "=C8+C9");

        $objPHPExcel->getActiveSheet()->setCellValue('B11', "Stok Akhir :");
        $objPHPExcel->getActiveSheet()->setCellValue('C11', $stok_akhir);

        $objPHPExcel->getActiveSheet()->getStyle('C11')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D11', "-");

        $objPHPExcel->getActiveSheet()->setCellValue('B12', "HPP :");
        $objPHPExcel->getActiveSheet()->setCellValue('C12', "=C10-C11");

        $objPHPExcel->getActiveSheet()->setCellValue('B14', "Penjualan :");
        $objPHPExcel->getActiveSheet()->setCellValue('C14', "$penjualan");

        $objPHPExcel->getActiveSheet()->setCellValue('B15', "HPP :");
        $objPHPExcel->getActiveSheet()->setCellValue('C15', "=C12");

        $objPHPExcel->getActiveSheet()->getStyle('C15')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->setCellValue('D15', "-");

        $objPHPExcel->getActiveSheet()->setCellValue('B16', "Rugi/Laba :");
        $objPHPExcel->getActiveSheet()->setCellValue('C16', "=C14-C15");

        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader("&L&G&C&HLaporan Rugi Laba");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=Laporan_Rugi_Laba_Toko_[$date]");
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}
?>